﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Sundry.Common
{
    public class CookieHelper
    {
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="cookieName">新 Cookie 的名称</param>
        /// <param name="cookieValue">新 Cookie 的值</param>
        public static void SetCookie(string cookieName,string cookieValue)
        {
            if (string.IsNullOrEmpty(cookieName) || string.IsNullOrEmpty(cookieValue)) return;
            if (System.Web.HttpContext.Current == null) return;
            HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
            cookie.HttpOnly = true;
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 设置Cookie 
        /// </summary>
        /// <param name="cookieName">新 Cookie 的名称</param>
        /// <param name="cookieValue">新 Cookie 的值</param>
        /// <param name="domain">获取将此 Cookie 与其关联的域</param>
        public static void SetCookie(string cookieName, string cookieValue, string domain)
         {
            if (string.IsNullOrEmpty(cookieName) || string.IsNullOrEmpty(cookieValue) || System.Web.HttpContext.Current == null) return;
            System.Web.HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Value = cookieValue;
            cookie.HttpOnly = true;
            cookie.Expires = DateTime.Now.AddHours(1);
            if (!string.IsNullOrEmpty(domain)) cookie.Domain = domain;
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 设置Cookie 
        /// </summary>
        /// <param name="cookieName">新 Cookie 的名称</param>
        /// <param name="cookieValue">新 Cookie 的值</param>
        /// <param name="domain">获取将此 Cookie 与其关联的域</param>
        /// <param name="timeType">设置有效时间类型（0 年，1天 2 小时）</param>
        /// <param name="timeValue">对应时间类型的值</param>
        public static void SetCookie(string cookieName, string cookieValue, string domain, int timeType, int timeValue)
        {
            if (string.IsNullOrEmpty(cookieName) || string.IsNullOrEmpty(cookieValue) || System.Web.HttpContext.Current == null) return;
            System.Web.HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
            cookie.HttpOnly = true;
            if (!string.IsNullOrEmpty(domain)) cookie.Domain = domain;
            if (timeType == 0)
            {
                cookie.Expires = DateTime.Now.AddYears(timeValue);
            }else if(timeType==1)
            {
                cookie.Expires = DateTime.Now.AddDays(timeValue);
            }
            else if (timeType == 2)
            {
                cookie.Expires = DateTime.Now.AddHours(timeValue);
            }
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 获取对应Cookie名称的值
        /// </summary>
        /// <param name="cookieName">Cookie 的名称</param>
        /// <returns></returns>
        public static string GetCookie(string cookieName)
        {
            if (string.IsNullOrEmpty(cookieName)) return string.Empty;
            if (System.Web.HttpContext.Current == null) return string.Empty;
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null) return string.Empty;
            else return System.Web.HttpContext.Current.Request.Cookies[cookieName].Value;
        }
        /// <summary>
        /// 判断对应的Cookie是否存在
        /// </summary>
        /// <param name="cookieName">Cookie 的名称</param>
        /// <returns></returns>
        public static bool ExistCookie(string cookieName)
        {
            
            if (string.IsNullOrEmpty(cookieName) || System.Web.HttpContext.Current == null) return false;
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null) return false;
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName].Value == null) return false;
            return (System.Web.HttpContext.Current.Request.Cookies[cookieName].Value.Length > 0);
        }
        /// <summary>
        /// 设置Cookie过期
        /// </summary>
        /// <param name="cookieName">Cookie的名称</param>
        public static void ExpireCookie(string cookieName)
        {
            if (string.IsNullOrEmpty(cookieName)) return;
            if (System.Web.HttpContext.Current != null)
            {
                HttpCookie cookie = new HttpCookie(cookieName, string.Empty);
                cookie.HttpOnly = true;
                cookie.Expires = DateTime.Now.AddYears(-15);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }
}
